package com.music.list;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.music.R;
import com.music.main.MainActivity;
import com.music.mp3.Mp3;
import com.music.player.Mp3PlayerActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AllMusicActivity extends ListActivity{
	//一个存放Mp3信息的集合
	public static List<Mp3> mp3list=null;
	int i=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.musiclist);
		mp3list=new ArrayList<Mp3>();
		//扫描SD卡下MP3文件夹中的Mp3歌曲，将扫描出的MP3对象存放到集合中
		File mp3File=new File(MainActivity.SDPath+"mp3/");
		File[] files=mp3File.listFiles();
		if(files!=null){
			for(File f:files){
				if(f.getName().endsWith("mp3")){
					Mp3 mp3=new Mp3();
					String[] str=f.getName().split("\\.");
					mp3.setMp3_name(str[0]);
					mp3.setId(i);
					i++;
					mp3list.add(mp3);
				}
			}
		}
	}
	//将MP3集合中的信息由列表形式显示
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		HashMap<String, String> map=null;
		List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
		if(mp3list.size()!=0){
			for(Mp3 m:mp3list){
				map=new HashMap<String, String>();
				map.put("mp3_name", m.getMp3_name());
				list.add(map);
			}
			SimpleAdapter adapter=new SimpleAdapter
					(this, list, R.layout.allmusic_item,  new String[]{"mp3_name"}, new int[]{R.id.mp3_name});
			setListAdapter(adapter);
		}
		super.onResume();
	}
	//点击列表元素，跳转到播放器页面
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(mp3list!=null){
			Intent i=new Intent();
			//传递点击MP3的下标
			i.putExtra("index", position);
			i.setClass(this, Mp3PlayerActivity.class);
			startActivity(i);
		}
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
	}
	
	
}
